/*****************************************************************
**                                                             	**
**  Copyright (C) 2004 Amlogic,Inc.                            	**
**  All rights reserved                                        	**
**        Filename : KeyBoard.c /Project:AVOS		       **
**        Revision : 1.0                                       	**
**                                                             	**
*****************************************************************/
#ifdef ENABLE_NETWORK
#include "../menu.h"
#include "KeyBoardEvent.h"
#include "../resource_network/network_icon_date.h"

INT32S WinKeyBoardMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
    switch( msg_type){
        case WM_SHOWWINDOW:
            return WinKeyBoardOnShow(hwnd, lpParam) ;
        case WM_HIDEWINDOW:
	     return WinKeyBoardOnHide(hwnd, lpParam) ;
    }
    return 0 ;
}

void SetWinKeyBoardPalette(void)
{
    YUVENTRY mask_para[4] = {0,4095,1092,4095};
    AWSetAlphaMask(0,4, mask_para);
    YUVENTRY palette_para[16] = {0x8000 | 16392,0x8000 | 16216,0x8000 | 18184,0x8000 | 16392,23970,18424,16088,16040,16024,15880,15208,15720,15016,15064,15304,15144};
    AWSetPalette(0,16, palette_para);
    return ;
}

void SetWinKeyBoardPos(HWND* pControl, INT8U CurOsdSize)
{

            RECT StandardWindowRect = {0,0,720,480};
            pControl->winrect = StandardWindowRect;
            CREATEWINDOWPARAM StandardWindowPara = {1,{{0,0,720,480},{0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},SetWinKeyBoardPalette};
            AWSendMessage(pControl, WM_WINDOW_RESET_DRAW_AREA, (INT32S)&StandardWindowPara);

    return;
}

HWND *CreateControlWinKeyBoard(HWND* pParent)
{
    HWND* pNewControl;
    CREATEWINDOWPARAM window_para = {1,{{0,0,720,480},{0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},SetWinKeyBoardPalette};
    //Create Menu Window -- 1
    pNewControl = AWCreateWinControl("TWindow","WinKeyBoard",WS_OVERLAPPEDWINDOW,
		         0,0,720,480,NULL, (INT32S)&window_para,WinKeyBoardMsgProc) ;
    pNewControl->win_id = 0 ;
    return pNewControl;
}

void SetKeyBoardCaptionPos(HWND* pControl, INT8U CurOsdSize)
{
    int iLoop;

            pControl->winrect.top = pControl->parent->winrect.top + 0;
            pControl->winrect.left = pControl->parent->winrect.left + 0;
            pControl->winrect.width = 720;
            pControl->winrect.height = 78;
            pControl->draw_info_num = 3;
            RECT StandardDrawRect[3] = {
            {pControl->winrect.left + 0,pControl->winrect.top + 0,136,78},
            {pControl->winrect.left + 600,pControl->winrect.top + 30,100,30},
            {pControl->winrect.left + 600,pControl->winrect.top + 30,100,28},
            };
            for(iLoop=0; iLoop < 3; iLoop++)
                memcpy(&(pControl->draw_info[iLoop]), &StandardDrawRect[iLoop], sizeof(RECT));
          
    return;
}

INT32S KeyBoardCaptionMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
    switch( msg_type){
        case WM_SHOWWINDOW:
            return KeyBoardCaptionOnShow(hwnd, lpParam) ;
        case WM_PAINT:
            return KeyBoardCaptionOnPaint(hwnd, lpParam) ;
    }
    return 0 ;
}

HWND *CreateControlKeyBoardCaption(HWND *pParent)
{
    //Create Control -- KeyBoardCaption
    HWND *pNewControl = AWCreateWinControl("TControl","KeyBoardCaption",(WS_CHILDWINDOW | WS_CHILDWINDOW),
		         0,0,720,78,pParent,0,KeyBoardCaptionMsgProc) ;
    pNewControl->win_id = 1 ;
    pNewControl->draw_info_num = 3;
    pNewControl->draw_info = (PDRAWINFO)AVMem_umalloc(sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
            DRAWINFO StandardDrawDatas[3] = {
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},
                          {0,0,0,0,(INT32U)0,DT_RLE_BMP_DATA},
                          {0,0,0,0,(INT32U)0,DT_RESOURCE_STRING_DATA}};
            memcpy(pNewControl->draw_info,StandardDrawDatas, sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
          
            return pNewControl;
}


void SetKeyControlBarPos(HWND* pControl, INT8U CurOsdSize)
{
    int iLoop;

            pControl->winrect.top = pControl->parent->winrect.top + 0;
            pControl->winrect.left = pControl->parent->winrect.left + 0;
            pControl->winrect.width = 720;
            pControl->winrect.height = 480;
            pControl->draw_info_num = 5;
            RECT StandardDrawRect[5] = {
#ifdef ENABLE_NETWORK 				
            {pControl->winrect.left + 0,pControl->winrect.top + 0,720,480},
            {pControl->winrect.left + 96,pControl->winrect.top + 80,566,36},
            {pControl->winrect.left + 2,pControl->winrect.top + 232,64,36},
            {pControl->winrect.left + 206,pControl->winrect.top + 332,300,36},
            {pControl->winrect.left + 100,pControl->winrect.top + 84,500,30},
#else
	     {pControl->winrect.left + 0,pControl->winrect.top + 0,720,480},
            {pControl->winrect.left + 96,pControl->winrect.top + 150,566,36},
            {pControl->winrect.left + 2,pControl->winrect.top + 232,64,36},
            {pControl->winrect.left + 206,pControl->winrect.top + 392,300,36},
            {pControl->winrect.left + 100,pControl->winrect.top + 154,500,30},
#endif
            };
            for(iLoop=0; iLoop < 5; iLoop++)
                memcpy(&(pControl->draw_info[iLoop]), &StandardDrawRect[iLoop], sizeof(RECT));

    return;
}

INT32S KeyControlBarMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
    switch( msg_type){
        case WM_SHOWWINDOW:
            return KeyControlBarOnShow(hwnd, lpParam) ;
        case WM_PAINT:
            return KeyControlBarOnPaint(hwnd, lpParam) ;
        case WM_KEYDOWN:
            return KeyControlBarOnKeyPress(hwnd, lpParam) ;
    }
    return 0 ;
}

HWND *CreateControlKeyControlBar(HWND *pParent)
{
    //Create Control -- KeyControlBar
    HWND *pNewControl = AWCreateWinControl("TControl","KeyControlBar",(WS_CHILDWINDOW | WS_CHILDWINDOW),
		         0,0,720,480,pParent,0,KeyControlBarMsgProc) ;
    pNewControl->win_id = 3 ;
    pNewControl->draw_info_num = 5;
    pNewControl->draw_info = (PDRAWINFO)AVMem_umalloc(sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
    DRAWINFO DrawDatas[5] = {
        {0,0,0,0,(INT32U)ICON_KEYBOARD,DT_RLE_BMP_DATA},
        {0,0,0,0,(INT32U)ICON_KEYBOARD_STRING,DT_RLE_BMP_DATA},
        {0,0,0,0,(INT32U)ICON_KEYBOARD_ONE,DT_RLE_BMP_DATA},
        {0,0,0,0,(INT32U)ICON_KEYBOARD_SPC_LOSE,DT_RLE_BMP_DATA},
        {0,0,0,0,(INT32U)Keyboard_str,DT_STRING_DATA}};
    memcpy(pNewControl->draw_info, DrawDatas, sizeof(DRAWINFO) * pNewControl->draw_info_num) ;

    return pNewControl;
}

HWND* CreateMenuWinKeyBoard(void)
{
    HWND *pNewControl ;
    TWINDOWS *pWin;
    HWND *pWinKeyBoard = CreateControlWinKeyBoard(NULL);
    SetWinKeyBoardPos(pWinKeyBoard,CurrentOsdSize);

    pNewControl= CreateControlKeyBoardCaption(pWinKeyBoard);
    SetKeyBoardCaptionPos(pNewControl,CurrentOsdSize);
    pNewControl= CreateControlKeyControlBar(pWinKeyBoard);
    SetKeyControlBarPos(pNewControl,CurrentOsdSize);

    pWin = (TWINDOWS *)pWinKeyBoard;
    pWin->pFocusControl = AWFindWinCtrlByName("KeyControlBar");
    return pWinKeyBoard;
}

void WinResetWinKeyBoardControlsPos(HWND *pControl, INT8U CurOsdSize)
{
    switch(pControl->win_id){
        case 0://WinKeyBoard
            SetWinKeyBoardPos(pControl,CurOsdSize) ;
            break ;
        case 1://KeyBoardCaption
            SetKeyBoardCaptionPos(pControl,CurOsdSize) ;
            break ;
        case 3://KeyControlBar
            SetKeyControlBarPos(pControl,CurOsdSize) ;
            break ;
    }//end of switch
}
void WinResetWinKeyBoardPos(HWND *pControl, INT8U CurOsdSize)
{
    HWND *pChildControl;
    if(pControl == NULL)
        pControl = AWFindWinCtrlByName("WinKeyBoard");
    if(pControl == NULL) return ;

    WinResetWinKeyBoardControlsPos(pControl, CurOsdSize) ;
    pChildControl = pControl->children ;
    while(pChildControl != NULL){
    	WinResetWinKeyBoardPos(pChildControl, CurOsdSize) ;
    	pChildControl = pChildControl->siblings ;
	}
	return ;
}
#endif
